package com.jilefojing.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jilefojing.beans.data.enums.ResponseEnum;
import com.jilefojing.beans.data.response.base.ResponseData;
import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.Comment;
import com.jilefojing.beans.pojos.enhanced.EnhancedComment;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.service.ICommentService;
import com.jilefojing.util.Detect;
import com.jilefojing.web.base.BaseWeb;
import com.jilefojing.web.convent.CommonConvent;

/**
 * 评论
 * 
 * @author jgshun
 * 
 */
@Controller
@RequestMapping("comment")
public class CommentWeb extends BaseWeb {
	@Autowired
	private ICommentService commentService;

	@RequestMapping("cancle")
	public @ResponseBody ResponseData cancle(Comment comment) {
		ResponseData responseData = new ResponseData();

		try {
			UserContext userContext = super.getUserContext();
			commentService.cancle(comment, userContext);
		} catch (UserContextException e) {
			if (e.getResponse() != null) {
				responseData.setResponseEnum(e.getResponse());
			} else {
				responseData.setResponseEnum(ResponseEnum.UNKNOWN);
				responseData.setResponseMessage(e.getMessage());
			}
		}

		return responseData;
	}

	@RequestMapping("add")
	public @ResponseBody ResponseData add(Comment comment) {
		ResponseData responseData = new ResponseData();

		try {
			UserContext userContext = super.getUserContext();
			comment = commentService.add(comment, userContext);
			responseData.setData(comment);
		} catch (UserContextException e) {
			if (e.getResponse() != null) {
				responseData.setResponseEnum(e.getResponse());
			} else {
				responseData.setResponseEnum(ResponseEnum.UNKNOWN);
				responseData.setResponseMessage(e.getMessage());
			}
		}

		return responseData;
	}

	/**
	 * 获取单条评论
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("get/{id}.html")
	public String getComment(@PathVariable("id") long id, HttpServletRequest request) {
		UserContext userContext = null;
		try {
			userContext = super.getUserContext();
		} catch (UserContextException e) {
			// 可忽略
		}

		EnhancedComment enhancedComment = commentService.getEnhancedComment(id, userContext);
		if (enhancedComment != null) {
			if (userContext != null) {
				enhancedComment = commentService.appendEnhancedAttitude2EnhancedCommentForUser(enhancedComment, userContext);
			}
			request.setAttribute("comment", CommonConvent.convent2CommentData(enhancedComment));
		}
		ItemTypeEnumForSource itemType = enhancedComment.getTopItemType() == null ? enhancedComment.getItemType() : enhancedComment.getTopItemType();
		return "web/comment/" + itemType.getItemClass().getSimpleName().toLowerCase() + "_comment_part";
	}

	/**
	 * 获取评论列表
	 * 
	 * @param itemType
	 *            类型
	 * @param itemId
	 *            评论对象
	 * @param type
	 *            要获取评论类型的类型 如：全部 热门 认证用户等
	 * @param index
	 *            页码
	 * @param request
	 * @return
	 */
	@RequestMapping("list-{itemType}-{itemId}.html")
	public String list(@PathVariable("itemType") short itemType, @PathVariable("itemId") long itemId, int type, int index, HttpServletRequest request) {
		UserContext userContext = null;
		try {
			userContext = super.getUserContext();
		} catch (UserContextException e) {
			// 可忽略
		}

		request.setAttribute("itemType", itemType);
		request.setAttribute("itemId", itemId);

		ItemTypeEnumForSource itemTypeEnumForComment = ItemTypeEnumForSource.getItemTypeEnumByCode(itemType);
		Pagination<EnhancedComment> enhancedCommentPagination = new Pagination<EnhancedComment>(index, 10, true);
		if (type == 0) {// 全部
			if (index == 1) {
				Pagination<EnhancedComment> enhancedHotCommentPagination = new Pagination<EnhancedComment>(1, 3, true);
				enhancedHotCommentPagination = commentService.getPaginationForHot(ItemTypeEnumForSource.getItemTypeEnumByCode(itemType), itemId, enhancedHotCommentPagination, userContext);
				if (Detect.notEmpty(enhancedHotCommentPagination.getItems())) {
					if (userContext != null) {
						enhancedHotCommentPagination.setItems(commentService.appendEnhancedAttitude2EnhancedCommentsForUser(enhancedHotCommentPagination.getItems(), userContext));
					}
					request.setAttribute("hotComments", CommonConvent.convent2CommentDatas(enhancedHotCommentPagination.getItems()));
				}
			}

			enhancedCommentPagination = commentService.getPaginationForActive(itemTypeEnumForComment, itemId, enhancedCommentPagination, userContext);
		} else if (type == 1) {// 热门
			enhancedCommentPagination = commentService.getPaginationForHot(itemTypeEnumForComment, itemId, enhancedCommentPagination, userContext);
		} else if (type == 2) {// 认证的用户 authentication
			// TODO
		} else if (type == 3) {// 关注的用户 attention
			// TODO
		} else if (type == 4) {// 陌生人 stranger
			// TODO
		}

		if (Detect.notEmpty(enhancedCommentPagination.getItems())) {
			if (userContext != null) {
				enhancedCommentPagination.setItems(commentService.appendEnhancedAttitude2EnhancedCommentsForUser(enhancedCommentPagination.getItems(), userContext));
			}

			request.setAttribute("enhancedCommentPagination", enhancedCommentPagination);
			request.setAttribute("comments", CommonConvent.convent2CommentDatas(enhancedCommentPagination.getItems()));
		}
		return "web/comment/" + itemTypeEnumForComment.getItemClass().getSimpleName().toLowerCase() + "_comment_list_part";

	}

	/**
	 * 获取单条对话
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("dialog/get/{id}.html")
	public String getDialog(@PathVariable("id") long id, HttpServletRequest request) {
		UserContext userContext = null;
		try {
			userContext = super.getUserContext();
		} catch (UserContextException e) {
			// 可忽略
		}

		EnhancedComment enhancedComment = commentService.getEnhancedComment(id, userContext);
		if (enhancedComment != null) {
			if (userContext != null) {
				enhancedComment = commentService.appendEnhancedAttitude2EnhancedCommentForUser(enhancedComment, userContext);
			}
			request.setAttribute("dialogComment", CommonConvent.convent2CommentData(enhancedComment));

		}
		return "web/comment/dialog_part";
	}

	/**
	 * 获取查看对话的评论列表
	 * 
	 * @param id
	 *            顶部评论id
	 * @param request
	 * @return
	 */
	@RequestMapping("dialog/list/{id}.html")
	public String getDialogs(@PathVariable("id") long id, HttpServletRequest request) {
		UserContext userContext = null;
		try {
			userContext = super.getUserContext();
		} catch (UserContextException e) {
			// 可忽略
		}

		List<EnhancedComment> enhancedChildCommentsForTop = commentService.getEnhancedDialogCommentsForTop(id, userContext);
		if (Detect.notEmpty(enhancedChildCommentsForTop)) {
			if (userContext != null) {
				enhancedChildCommentsForTop = commentService.appendEnhancedAttitude2EnhancedCommentsForUser(enhancedChildCommentsForTop, userContext);
			}
			request.setAttribute("dialogComments", CommonConvent.convent2CommentDatas(enhancedChildCommentsForTop));
		}
		return "web/comment/dialog_list";
	}
}
